<?php

// 发立得信息发布系统房屋信息版(php+mysql) V1.0
// 演示地址: http://fang.chalide.cn
// 文件路径: index.php
// 文件大小: 15611 字节
// 最后修改时间: 2025-06-09 18:44:17
// 作者: yujianyue
// 邮件: 15058593138@qq.com
// 版权所有,保留发行权和署名权
/**
 * 本文件功能：信息列表1 - 列表式房屋信息展示
 * 版权声明：保留发行权和署名权
 * 作者信息：15058593138@qq.com
 */

$page_title = '房屋信息列表';
$show_nav = false;

// 处理AJAX请求
if (isset($_GET['act'])) {
    require_once './inc/conn.php';
    require_once './inc/pubs.php';
    require_once './sqls.php';
    
    $act = $_GET['act'];
    $db = DB::getInstance();
    
    switch ($act) {
        // 获取信息列表
        case 'list':
            $page = isset($_POST['page']) ? intval($_POST['page']) : 1;
            $limit = 10;
            
            // 构建查询条件
            $where = 'open1 = 1 AND open2 = 1';
            
            if (!empty($_POST['county'])) {
                $where .= " AND county = '" . $db->escape($_POST['county']) . "'";
            }
            
            if (!empty($_POST['type1'])) {
                $where .= " AND type1 = " . intval($_POST['type1']);
            }
            
            if (!empty($_POST['type2'])) {
                $where .= " AND type2 = " . intval($_POST['type2']);
            }
            
            if (!empty($_POST['keyword'])) {
                $keyword = $db->escape($_POST['keyword']);
                $where .= " AND (title LIKE '%$keyword%' OR `desc` LIKE '%$keyword%' OR `keys` LIKE '%$keyword%')";
            }
            
            // 查询总数
            $total = $db->count('info', $where);
            
            // 分页参数
            $pagination = get_pagination($total, $page, $limit);
            
            // 查询列表
            $sql = "SELECT i.*, u.user FROM info i 
                    LEFT JOIN user u ON i.user_id = u.id 
                    WHERE $where 
                    ORDER BY i.gai_time DESC 
                    LIMIT {$pagination['offset']}, {$pagination['limit']}";
            
            $list = $db->getAll($sql);
            
            // 处理列表数据
            foreach ($list as &$item) {
                // 处理时间格式
                $item['add_time'] = format_datetime($item['add_time'], 'Y-m-d H:i');
                $item['gai_time'] = format_datetime($item['gai_time'], 'Y-m-d H:i');
                
                // 获取第一张图片
                $img_sql = "SELECT path FROM imgs WHERE info_id = {$item['id']} ORDER BY sort ASC, id ASC LIMIT 1";
                $img = $db->getOne($img_sql);
                $item['img'] = $img ? $img : 'imgs/default.jpg';
                
                // 加载类型文本
                $type1_json = file_get_contents('./inc/type1.json');
                $type2_json = file_get_contents('./inc/type2.json');
                $type1_arr = json_decode($type1_json, true);
                $type2_arr = json_decode($type2_json, true);
                
                $item['type1_text'] = isset($type1_arr[$item['type1']]) ? $type1_arr[$item['type1']] : '';
                $item['type2_text'] = isset($type2_arr[$item['type2']]) ? $type2_arr[$item['type2']] : '';
                
                // 生成查看详情token
                $item['token'] = md5($item['id'] . 'info_detail');
            }
            
            // 返回数据
            json_msg(1, '获取成功', [
                'list' => $list,
                'page' => $pagination['page'],
                'total_page' => $pagination['total_page'],
                'total' => $pagination['total']
            ]);
            break;
            
        // 获取信息详情
        case 'detail':
            if (empty($_POST['id']) || empty($_POST['token'])) {
                json_msg(0, '参数错误');
            }
            
            $id = intval($_POST['id']);
            $token = $_POST['token'];
            
            // 验证token
            if ($token !== md5($id . 'info_detail')) {
                json_msg(0, '无效的请求');
            }
            
            // 更新浏览次数
            $db->query("UPDATE info SET view = view + 1 WHERE id = $id");
            
            // 查询详情
            $sql = "SELECT i.*, u.user FROM info i 
                    LEFT JOIN user u ON i.user_id = u.id 
                    WHERE i.id = $id AND i.open1 = 1 AND i.open2 = 1";
            
            $info = $db->getRow($sql);
            
            if (!$info) {
                json_msg(0, '信息不存在或已下架');
            }
            
            // 处理时间格式
            $info['add_time'] = format_datetime($info['add_time'], 'Y-m-d H:i');
            $info['gai_time'] = format_datetime($info['gai_time'], 'Y-m-d H:i');
            
            // 获取图片
            $img_sql = "SELECT id, path FROM imgs WHERE info_id = {$info['id']} ORDER BY sort ASC, id ASC";
            $info['imgs'] = $db->getAll($img_sql);
            
            // 加载类型文本
            $type1_json = file_get_contents('./inc/type1.json');
            $type2_json = file_get_contents('./inc/type2.json');
            $type1_arr = json_decode($type1_json, true);
            $type2_arr = json_decode($type2_json, true);
            
            $info['type1_text'] = isset($type1_arr[$info['type1']]) ? $type1_arr[$info['type1']] : '';
            $info['type2_text'] = isset($type2_arr[$info['type2']]) ? $type2_arr[$info['type2']] : '';
            
            // 处理关键词
            $info['keys_arr'] = $info['keys'] ? explode(',', $info['keys']) : [];
            
            json_msg(1, '获取成功', $info);
            break;
            
        default:
            json_msg(0, '未知操作');
    }
    
    exit;
}

include './inc/head.php';
?>

<div class="top-bar">
    <div class="title">房屋信息列表</div>
    <div class="right-btns">
        <a href="usr.php?do=fabu">发布信息</a>
    </div>
</div>

<div class="search-bar">
    <div class="search-item">
        <select id="county">
            <option value="">所有区县</option>
            <?php
            // 获取区县列表
            $area_file = './inc/area.json'; // 默认杭州
            if (file_exists($area_file)) {
                $area_json = file_get_contents($area_file);
                $area_arr = json_decode($area_json, true);
                foreach ($area_arr as $code => $name) {
                    echo "<option value=\"$code\">$name</option>";
                }
            }
            ?>
        </select>
    </div>
    
    <div class="search-item">
        <select id="type1">
            <option value="">所有信息类型</option>
            <?php
            // 获取信息类型
            $type1_file = './inc/type1.json';
            if (file_exists($type1_file)) {
                $type1_json = file_get_contents($type1_file);
                $type1_arr = json_decode($type1_json, true);
                foreach ($type1_arr as $code => $name) {
                    echo "<option value=\"$code\">$name</option>";
                }
            }
            ?>
        </select>
    </div>
    
    <div class="search-item">
        <select id="type2">
            <option value="">所有交易类型</option>
            <?php
            // 获取交易类型
            $type2_file = './inc/type2.json';
            if (file_exists($type2_file)) {
                $type2_json = file_get_contents($type2_file);
                $type2_arr = json_decode($type2_json, true);
                foreach ($type2_arr as $code => $name) {
                    echo "<option value=\"$code\">$name</option>";
                }
            }
            ?>
        </select>
    </div>
    
    <div class="search-item">
        <input type="text" id="keyword" placeholder="关键词搜索">
    </div>
    
    <div class="search-btns">
        <button type="button" class="btn btn-primary" id="search-btn">搜索</button>
    </div>
</div>

<div id="info-list"></div>
<div id="pagination"></div>

<script>
// 页面加载完成后执行
document.addEventListener('DOMContentLoaded', function() {
    // 加载信息列表
    loadInfoList();
    
    // 搜索按钮点击事件
    document.getElementById('search-btn').addEventListener('click', function() {
        loadInfoList();
    });
    
    // 回车搜索
    document.getElementById('keyword').addEventListener('keydown', function(e) {
        if (e.keyCode === 13) {
            loadInfoList();
        }
    });
});

// 加载信息列表
function loadInfoList(page) {
    page = page || 1;
    
    // 获取搜索参数
    const county = document.getElementById('county').value;
    const type1 = document.getElementById('type1').value;
    const type2 = document.getElementById('type2').value;
    const keyword = document.getElementById('keyword').value;
    
    // 显示加载中
    document.getElementById('info-list').innerHTML = '<div style="text-align:center;padding:20px;">加载中...</div>';
    
    // 请求数据
    ajax({
        url: 'index.php?act=list',
        method: 'POST',
        data: {
            page: page,
            county: county,
            type1: type1,
            type2: type2,
            keyword: keyword
        },
        success: function(res) {
            if (res.code === 1) {
                // 渲染列表
                renderInfoList(res.data.list);
                
                // 渲染分页
                document.getElementById('pagination').innerHTML = renderPagination(res.data.page, res.data.total_page);
                
                // 绑定分页事件
                bindPaginationEvents(function(page) {
                    loadInfoList(page);
                });
                
                // 绑定详情查看事件
                bindDetailEvents();
            } else {
                document.getElementById('info-list').innerHTML = '<div style="text-align:center;padding:20px;">暂无数据</div>';
                document.getElementById('pagination').innerHTML = '';
                showToast(res.msg);
            }
        },
        error: function() {
            document.getElementById('info-list').innerHTML = '<div style="text-align:center;padding:20px;">加载失败，请刷新重试</div>';
            document.getElementById('pagination').innerHTML = '';
            showToast('网络错误，请稍后重试');
        }
    });
}

// 渲染信息列表
function renderInfoList(list) {
    if (!list || list.length === 0) {
        document.getElementById('info-list').innerHTML = '<div style="text-align:center;padding:20px;">暂无数据</div>';
        return;
    }
    
    let html = '<table class="table">';
    html += '<tr><th width="20%">标题</th><th width="15%">类型</th><th width="15%">价格/面积</th><th width="20%">位置</th><th width="15%">发布时间</th><th width="15%">操作</th></tr>';
    
    for (let i = 0; i < list.length; i++) {
        const item = list[i];
        html += '<tr>';
        html += '<td>' + item.title + '</td>';
        html += '<td>' + item.type1_text + '/' + item.type2_text + '</td>';
        html += '<td>' + item.jiage + '元' + (item.area ? '/' + item.area + '㎡' : '') + '</td>';
        html += '<td>' + (item.addr || '未填写') + '</td>';
        html += '<td>' + item.gai_time + '</td>';
        html += '<td><a href="javascript:;" class="btn-detail" data-id="' + item.id + '" data-token="' + item.token + '">查看详情</a></td>';
        html += '</tr>';
    }
    
    html += '</table>';
    
    document.getElementById('info-list').innerHTML = html;
}

// 绑定详情查看事件
function bindDetailEvents() {
    const detailBtns = document.querySelectorAll('.btn-detail');
    
    for (let i = 0; i < detailBtns.length; i++) {
        detailBtns[i].addEventListener('click', function() {
            const id = this.getAttribute('data-id');
            const token = this.getAttribute('data-token');
            
            // 显示加载中
            showModal('信息详情', '<div style="text-align:center;padding:20px;">加载中...</div>', []);
            
            // 请求详情
            ajax({
                url: 'index.php?act=detail',
                method: 'POST',
                data: {
                    id: id,
                    token: token
                },
                success: function(res) {
                    if (res.code === 1) {
                        // 渲染详情
                        renderInfoDetail(res.data);
                    } else {
                        showToast(res.msg);
                    }
                },
                error: function() {
                    showToast('网络错误，请稍后重试');
                }
            });
        });
    }
}

// 渲染信息详情
function renderInfoDetail(info) {
    let html = '<div class="info-detail">';
    
    // 标题
    html += '<h2 style="margin-bottom:15px;text-align:center;">' + info.title + '</h2>';
    
    // 图片
    if (info.imgs && info.imgs.length > 0) {
        html += '<div class="info-images" style="margin-bottom:15px;text-align:center;">';
        for (let i = 0; i < info.imgs.length; i++) {
            html += '<img src="' + info.imgs[i].path + '" style="max-width:100%;max-height:300px;margin:5px;">';
        }
        html += '</div>';
    }
    
    // 基本信息
    html += '<div class="info-base" style="margin-bottom:15px;padding:10px;background:#f5f5f5;border-radius:4px;">';
    html += '<p><strong>信息类型：</strong>' + info.type1_text + '</p>';
    html += '<p><strong>交易类型：</strong>' + info.type2_text + '</p>';
    html += '<p><strong>价格：</strong>' + info.jiage + '元</p>';
    if (info.area) {
        html += '<p><strong>面积：</strong>' + info.area + '㎡</p>';
    }
    if (info.addr) {
        html += '<p><strong>位置：</strong>' + info.addr + '</p>';
    }
    html += '<p><strong>浏览次数：</strong>' + info.view + '</p>';
    html += '<p><strong>发布时间：</strong>' + info.add_time + '</p>';
    html += '<p><strong>更新时间：</strong>' + info.gai_time + '</p>';
    html += '</div>';
    
    // 详细描述
    if (info.desc) {
        html += '<div class="info-desc" style="margin-bottom:15px;">';
        html += '<h3>详细描述</h3>';
        html += '<div style="padding:10px;background:#f9f9f9;border-radius:4px;">' + info.desc.replace(/\n/g, '<br>') + '</div>';
        html += '</div>';
    }
    
    // 关键词
    if (info.keys_arr && info.keys_arr.length > 0) {
        html += '<div class="info-keys" style="margin-bottom:15px;">';
        html += '<h3>关键词</h3>';
        html += '<div style="padding:10px;background:#f9f9f9;border-radius:4px;">';
        for (let i = 0; i < info.keys_arr.length; i++) {
            html += '<span style="display:inline-block;margin:2px 5px;padding:2px 8px;background:#e1e1e1;border-radius:3px;">' + info.keys_arr[i] + '</span>';
        }
        html += '</div></div>';
    }
    
    // 联系方式
    html += '<div class="info-contact" style="margin-bottom:15px;">';
    html += '<h3>联系方式</h3>';
    html += '<div style="padding:10px;background:#f9f9f9;border-radius:4px;">';
    html += '<p><strong>联系人：</strong>' + info.cname + '</p>';
    html += '<p><strong>联系电话：</strong>' + info.cmobi + '</p>';
    html += '</div></div>';
    
    html += '</div>';
    
    // 更新模态框内容
    const modalBody = document.querySelector('.modal-body');
    if (modalBody) {
        modalBody.innerHTML = html;
    }
}
</script>

<?php include './inc/foot.php'; ?>
